<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2020/4/26
 * Time: 17:13
 */

namespace app\common\service;


class FileAttack
{

    /**
     * @name: 列表
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-08-01 16:38:58
     */
    function list ($filterData, $limit)
    {

        $server = db('FileAttack');

        !isset($filterData['host_id']) || empty($filterData['host_id']) ? : $server->whereIn('host_id', $filterData['host_id']);
        !isset($filterData['source']) || empty($filterData['source']) ? : $server->where('source', $filterData['source']);
        !isset($filterData['description']) || empty($filterData['description']) ? : $server->whereLike('description', '%'. $filterData['description'] . '%');
        !isset($filterData['dest_ip']) || empty($filterData['dest_ip']) ? : $server->whereLike('dest_ip', '%'. $filterData['dest_ip'] .'%');
        !isset($filterData['path']) || empty($filterData['path']) ? : $server->whereLike('path', '%'. $filterData['path'] .'%');
        if (isset($filterData['atime']) && !empty($filterData['atime']))
        {
            $stime = strtotime($filterData['atime']);
            $etime = $stime + 86400;
            $server->whereBetween('atime', [$stime,$etime]);
        }


        !isset($filterData['ctime']) && empty($filterData['ctime']) ? : $server->where('ctime', $filterData['ctime']);
        !isset($filterData['type']) || !is_numeric($filterData['type']) ? : $server->where('type', $filterData['type']);
        !isset($filterData['level']) || !is_numeric($filterData['level']) ? : $server->where('level', $filterData['level']);
        !isset($filterData['handle']) || !is_numeric($filterData['handle']) ? : $server->where('handle', $filterData['handle']);
        !isset($filterData['reset']) || !is_numeric($filterData['reset']) ? : $server->where('reset', $filterData['reset']);

        $return = $server->paginate($limit)->toArray();

        return $return;
    }

    /**
     * @name: 恢复
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-08-09 10:04:24
     */
    function rest ($id)
    {

        db('FileAttack')->where('id', $id)->update([
            'reset' => model('FileAttack')->RESTORED
        ]);
    }

    /**
     * @name: 清空事件
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-08-20 16:53:55
     */
    function clear ($host_id)
    {
        db('FileAttack')->whereIn('host_id', $host_id)->delete();
    }

    /**
     * @name: 批量删除
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-08-20 16:53:55
     */
    function del ($id)
    {
        db('FileAttack')->whereIn('id', $id)->delete();
    }

    /**
     * @name: 统计
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-09-05 15:20:10
     */
    function ratio ($source, $host_id)
    {
        $return = db('FileAttack')->whereIn('source', $source)->whereIn('host_id', $host_id)->count();

        return $return;
    }

    /**
     * @name: 检查
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-09-27 10:39:18
     */
    function findOne ($id)
    {
        $return = db('FileAttack')->find($id);

        return $return;
    }


}